package com.bachongLightNovel.audit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bachongLightNovel.audit.mapper.AuditLogsMapper;
import com.bachongLightNovel.audit.service.AuditLogsService;
import com.bachongLightNovel.model.dto.audit.AuditLogsDto;
import com.bachongLightNovel.model.pojo.audit.AuditLogs;
import com.bachongLightNovel.model.vo.page.PageVo;
import com.bachongLightNovel.model.vo.audit.AuditLogsVo;
import com.bachongLightNovel.system.ISystemUserClient;
import com.bachongLightNovel.works.IWorksNovelClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author vermouth
 */
@Service
@Slf4j
public class AuditLogsServiceImpl implements AuditLogsService {

    @Autowired
    private ISystemUserClient systemUserClient;

    @Autowired
    private IWorksNovelClient worksNovelClient;

    @Autowired
    private AuditLogsMapper auditLogsMapper;

    /**
     * 审核日志分页列表查询
     * @param current : 当前页
     * @param size    : 每页显示记录数
     * @param dto     : 查询条件
     * @return Result
     */
    @Override
    public PageVo<AuditLogsVo> findPage(Integer current, Integer size, AuditLogsDto dto) {
        //todo  当前查询效率很慢， 日后看有没有可以优化的地方
        List<Long> userIds = new ArrayList<>();
        List<Long> novelIds = new ArrayList<>();
        String auditUser = dto.getAuditUser();
        String novelName = dto.getNovelName();
        //判断auditUser和novelName是否为空
        if (!StrUtil.isBlank(auditUser)) {
            userIds = systemUserClient.getUserIdByUsername(auditUser);
            log.info("审核人员id：{}", userIds);
        }
        //不为空，则调用feign接口获取对应的id
        if (StrUtil.isNotBlank(novelName)) {
            novelIds = worksNovelClient.getNovelIdByNovelName(novelName);
            log.info("小说id:{}", novelIds);
        }
        if (!userIds.isEmpty()) {
            dto.setUserIds(userIds);
        }
        if (!novelIds.isEmpty()) {
            dto.setNovelIds(novelIds);
        }
        //查询总的记录数
        Long total = auditLogsMapper.selectCount();
        //按照分页条件，查询审核日志数据
        List<AuditLogs> auditLogsList = auditLogsMapper.selectByQuery((current - 1) * size, size, dto);
        //封装vo相应属性
        List<AuditLogsVo> voList = new ArrayList<>();
        for (AuditLogs auditLogs : auditLogsList) {
            AuditLogsVo auditLogsVo = new AuditLogsVo();
            BeanUtils.copyProperties(auditLogs, auditLogsVo);
            auditLogsVo.setAuditUser(systemUserClient.getUsernameById(auditLogs.getUserId()));
            auditLogsVo.setNovelName(worksNovelClient.getNovelNameById(auditLogs.getNovelId()));
            voList.add(auditLogsVo);
        }
        //封装PageVo对象返回
        PageVo<AuditLogsVo> pageVo = new PageVo<>();
        pageVo.setTotal(total);
        pageVo.setRecords(voList);
        return pageVo;
    }


    /**
     * 根据ID删除审核日志（逻辑删除）
     * @param id :
     * @return Result
     */
    @Override
    public void removeLog(Long id) {
        //根据id修改数据库的is_delete字段，采用逻辑删除的方式
        auditLogsMapper.updateDelete(id);
    }
}
